
package Final;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Board extends JFrame implements ActionListener{

  private static Panel panel = new Panel();
  private static boolean bpanel;
  private static int alto;
  private static int ancho;
  private static JMenuItem manual, abrir, close, guardar, limpiar, dimensiones;



  public static void main (String[] args){
	  new Board();
  }
  
  public void creaMenu(){
	    JMenuBar mb = new JMenuBar();
	    setJMenuBar(mb);
	    JMenu menu = new JMenu("Archivo");
	    mb.add(menu);

	    menu.add(manual = new JMenuItem("Nuevo"));
	    menu.add(new JSeparator());
	    menu.add(abrir = new JMenuItem("Abrir"));
	    menu.add(guardar = new JMenuItem("Guardar"));
	    menu.add(new JSeparator());
	    menu.add(limpiar = new JMenuItem("Limpiar"));
	    menu.add(new JSeparator());
	    menu.add(close = new JMenuItem("Cerrar"));



	    manual.addActionListener(this);
	    abrir.addActionListener(this);
	    guardar.addActionListener(this);
	    limpiar.addActionListener(this);
	    close.addActionListener(this);
	    bpanel = false;
	    
	    setVisible(true);
  }
  
  public Board(){ 
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    alto = screenSize.height;
    ancho = screenSize.width;

    setSize(ancho, alto);
    this.setExtendedState(this.MAXIMIZED_BOTH);
    setLocation(ancho/4, alto/4);

    creaMenu();

  }

  public void setDimensiones(int fil, int col){
	  setSize(ancho, alto);
	  this.setExtendedState(this.MAXIMIZED_BOTH);
  }
  
  private void ejecAbrir() {
		String s = (String)JOptionPane.showInputDialog(null, "Nombre del fichero:\n", "Input", JOptionPane.PLAIN_MESSAGE);
		if ((s != null) && (s.length() > 0)) { 
			if(!bpanel){
				getContentPane().add(panel, BorderLayout.CENTER);
				bpanel = true;
			}
			if(!panel.cargarMatriz(s))
				System.out.println("No se encuentra el fichero");
			else
				setDimensiones(panel.getFil(), panel.getCol());
		}else{
			System.out.println("Escriba nombre");
		}	
		
	}
  
  private void ejecNuevo() {
		try{
			int fil;
			do{
				fil = Integer.parseInt((String)(JOptionPane.showInputDialog(Board.this, "FILAS ", "Filas", JOptionPane.QUESTION_MESSAGE, null, null,20)));
			} while (fil < Matriz.MINFIL || fil > Matriz.MAXFIL);
			int col;
			do{
				col =  Integer.parseInt((String)(JOptionPane.showInputDialog(Board.this, "COLUMNAS ", "Columnas", JOptionPane.QUESTION_MESSAGE, null, null,20)));
			} while (col < Matriz.MINCOL || col > Matriz.MAXCOL);
		
			if(bpanel)
				panel.reset();
			else{
				getContentPane().add(panel, BorderLayout.CENTER);
				bpanel = true;
			}
			panel.setFil(fil);
	    		panel.setCol(col);
	    		setDimensiones(fil, col);
		}catch(Exception en){
			
		}
		
	}
  
  private void ejecDimensi() {
		panel.reset();
		try{
			int fil;
			do{
				fil = Integer.parseInt((String)(JOptionPane.showInputDialog(Board.this, "FILAS", "Filas", JOptionPane.QUESTION_MESSAGE, null, null, 20)));
			} while (fil < Matriz.MINFIL || fil > Matriz.MAXFIL);
			int col;
			do{
				col =  Integer.parseInt((String)(JOptionPane.showInputDialog(Board.this, "COLUMNAS", "Columnas", JOptionPane.QUESTION_MESSAGE, null, null, 20)));
			} while (col < Matriz.MINCOL || col > Matriz.MAXCOL);
			
			if(bpanel){
				panel.setFil(fil);
				panel.setCol(col);
	    			setDimensiones(fil, col);
			}else
				System.out.println("No hay tablero");
		}catch(Exception en){
			
		}
		
	}
  
  private void ejecGuardar() {
		String s = (String)JOptionPane.showInputDialog(null, "Nombre del fichero:\n", "Input", JOptionPane.PLAIN_MESSAGE);
		if ((s != null) && (s.length() > 0) && bpanel) {
			if(!panel.guardarMatriz(s))
				System.out.println("No se encuentra el fichero");
		}else{
			System.out.println("Error en nombre o tablero");
		}
		
	}

  public void actionPerformed(ActionEvent e) {
	  String actionCommand = e.getActionCommand();
	  if (e.getSource() instanceof JMenuItem) {
    	if ("Abrir".equals(actionCommand)){
    		ejecAbrir();
    	}else if ("Guardar".equals(actionCommand)){
    		ejecGuardar();
    	}else if ("Limpiar".equals(actionCommand))
    		panel.reset();
        else if ("Dimensiones".equals(actionCommand)){
        	ejecDimensi();
        	
        }else if("Nuevo".equals(actionCommand)){
        	ejecNuevo();
        }
        else if ("Cerrar".equals(actionCommand))
            System.exit(0);	
    }
  }








}
